package com.mach.platform.service;

import com.mach.platform.domain.mach.QuotaRecord;
import com.mach.platform.repository.QuotaRecordRepo;
import com.mach.platform.utils.DateUtil;
import com.mach.platform.utils.security.SecurityUtils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/6/5 0005 16:09
 */
@Service
public class QuotaRecordServiceImpl  implements QuotaRecordService{

	@Autowired
	private QuotaRecordRepo quotaRecordRepo;


	/**
	 * 保存
	 *
	 * @param quotaRecord 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public QuotaRecord save (QuotaRecord quotaRecord) {

		quotaRecord.setCreateUser(SecurityUtils.getCurrentUserName());
		quotaRecord.setCreateTime(DateUtil.getYMDHMS());

		return quotaRecordRepo.saveAndFlush(quotaRecord);
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Override
	public List<QuotaRecord> insertObjInBatch (List<QuotaRecord> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param quotaRecord 跟新的实体
	 * @return T
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public QuotaRecord update (QuotaRecord quotaRecord) {

		quotaRecord.setUpdUser(SecurityUtils.getCurrentUserName());
		quotaRecord.setUpdTime(DateUtil.getYMDHMS());

		return quotaRecordRepo.saveAndFlush(quotaRecord);
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public int delete (Long[] ids) {
		return quotaRecordRepo.deleteByIdIn(ids);
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<QuotaRecord> list (Pageable pageable, Map params) {
		Specification specification = DynamicSpecifications
				                              .bySearchFilter(SearchFilter.parse(params).values(), QuotaRecord.class);

		return quotaRecordRepo.findAll(specification, pageable);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public QuotaRecord info (Long id) {
		return quotaRecordRepo.getById(id);
	}

	/**
	 * 导入数据
	 *
	 * @param models
	 * @return
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public int importDatas (List<QuotaRecord> models) {

		if (models.size() == 0) {
			return 0;
		}

		List<QuotaRecord> insertObjs = models.stream().filter(item -> item.getFyear() != null && item.getNum() !=null
				                                                         && item.getFactory() != null).map(dev -> {
			dev.setCreateUser(SecurityUtils.getCurrentUserName());
			dev.setCreateTime(DateUtil.getYMDHMS());
			return dev;
		}).collect(Collectors.toList());

		List<QuotaRecord> iotDevs = quotaRecordRepo.saveAll(insertObjs);

		return iotDevs.size();
	}

}
